package com.github.kezhenxu94.playground.java.concurrent.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <pre>
 * 
 * {@link ThreadPoolExecutor} 类实现了 {@link ExecutorService}
 * 接口, 使用已经缓存的线程对象来执行提交的任务, 该类可以配置一个 
 * {@link RejectedExecutionHandler} 来处理当 Executor 被关闭后, 
 * 新任务被提交时应该采取的操作, 可以通过构造函数或 setter 方法设置该处理
 * 器.
 * 
 * </pre>
 * 
 * @author kezhenxu
 *
 */
public class StudyThreadPoolExecutor {
	
	public static void main(String[] args) {
		ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
		
		executor.setRejectedExecutionHandler((r, exec) -> System.out.println("ThreadPoolExecutor is shutdown"));
		
		executor.execute(() -> System.out.println("I can run because the executor is active"));
		executor.shutdown();
		executor.execute(() -> System.out.println("I can not run"));
	}
}
